package frontend;

import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListModel;

import dbmanager.AccountManager;
import dbmanager.FoodManager;
import dbmanager.RecipeManager;
import framework.FoodItem;
import framework.Recipe;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RecipeGUI extends javax.swing.JPanel {
	private JList recipeList;
	private JButton addRecipeButton;
	private JLabel filterLabel;
	private JComboBox jComboBox1;
	private JCheckBox approvedBox;
	private JButton approveButton;
	private JTextPane stepsPane;
	private JTextPane ingredientPane;
	private JLabel infoLabel;
	private JLabel recipeName;
	private JCheckBox jCheckBox1;
	private JLabel viewLabel;
	private JCheckBox prefsBox;
	private JLabel recipeLabel;
	private JTextField recipeFilterField;
	private JScrollPane jScrollPane1;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new RecipeGUI());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public RecipeGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(968, 734));
			this.setSize(968, 734);
			this.setBackground(new java.awt.Color(255,255,255));
			this.setLayout(null);
			{
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							new String[] { "Name", "Ingredient" });
				jComboBox1 = new JComboBox();
				this.add(jComboBox1);
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.setBounds(318, 45, 121, 27);
			}
			{
				prefsBox = new JCheckBox();
				this.add(prefsBox);
				prefsBox.setText("Recipes I can cook");
				prefsBox.setBackground(new java.awt.Color(255,255,255));
				prefsBox.setBounds(49, 683, 251, 28);
				prefsBox.setVisible(false);
				prefsBox.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							prefsBoxMouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				approvedBox = new JCheckBox();
				this.add(approvedBox);
				approvedBox.setText("Approved recipes");
				approvedBox.setBounds(306, 658, 135, 22);
				approvedBox.setSelected(true);
				approvedBox.setVisible(false);
				if(AccountManager.getLogin().isAdmin()){
					approvedBox.setVisible(true);
					approvedBox.setBackground(new java.awt.Color(255,255,255));
					approvedBox.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								approvedBoxMouseClicked(evt);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(39, 78, 400, 538);
				{
				
					recipeList = new JList();
					jScrollPane1.setViewportView(recipeList);
					getRecipesLike("%");
					recipeList.setBounds(45, 67, 262, 403);
					recipeList.setBorder(BorderFactory.createTitledBorder(""));
					recipeList.setFixedCellHeight(60);
					recipeList.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								recipeListMouseClicked(evt);
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
			}
			{
				recipeFilterField = new JTextField();
				this.add(recipeFilterField);
				recipeFilterField.setBounds(39, 45, 267, 27);
				recipeFilterField.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						try {
							recipeFilterFieldKeyPressed(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				recipeLabel = new JLabel();
				this.add(recipeLabel);
				recipeLabel.setText("Recipe Search");
				recipeLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,14));
				recipeLabel.setBounds(39, 12, 174, 27);
			}
			
			{
				viewLabel = new JLabel();
				this.add(viewLabel);
				viewLabel.setText("View Only:");
				viewLabel.setFont(new java.awt.Font("AlArabiya",1,12));
				viewLabel.setBounds(38, 628, 80, 27);
			}
			{
				jCheckBox1 = new JCheckBox();
				this.add(jCheckBox1);
				jCheckBox1.setText("Recipes that suit my preferences");
				jCheckBox1.setBackground(new java.awt.Color(255,255,255));
				jCheckBox1.setBounds(49, 655, 251, 28);
				jCheckBox1.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							jCheckBox1MouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				recipeName = new JLabel();
				this.add(recipeName);
				recipeName.setBounds(465, 42, 448, 31);
				recipeName.setFont(new java.awt.Font("Bitstream Vera Sans",1,18));
			}
			{
				infoLabel = new JLabel();
				this.add(infoLabel);
				infoLabel.setText("Recipe Information");
				infoLabel.setBounds(465, 12, 201, 26);
				infoLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,14));
			}
			{
				ingredientPane = new JTextPane();
				this.add(ingredientPane);
				ingredientPane.setText("Ingredients");
				ingredientPane.setBounds(465, 79, 460, 267);
				ingredientPane.setContentType("text/html");
				ingredientPane.setEditable(false);
			}
			{
				stepsPane = new JTextPane();
				this.add(stepsPane);
				stepsPane.setText("Steps");
				stepsPane.setBounds(465, 372, 460, 292);
				stepsPane.setContentType("text/html");
				stepsPane.setEditable(false);
			}
			{
				approveButton = new JButton();
				this.add(approveButton);
				approveButton.setText("Approve");
				approveButton.setBounds(629, 670, 150, 37);
				approveButton.setVisible(false);
				approveButton.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							approveButtonMouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			
			{
				filterLabel = new JLabel();
				this.add(filterLabel);
				filterLabel.setText("Filter by:");
				filterLabel.setBounds(318, 17, 76, 22);
			}
			{
				addRecipeButton = new JButton();
				this.add(addRecipeButton);
				addRecipeButton.setText("Add a Recipe");
				addRecipeButton.setBounds(801, 11, 124, 27);
				addRecipeButton.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						addRecipeButtonMouseClicked(evt);
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getRecipesLike(String like) throws SQLException{
		if(prefsBox.isSelected()){
			updateRecipeList(RecipeManager.fetchRecipesSuitingPreferences(like, approvedBox.isSelected()));
		} else {
			if(((String)jComboBox1.getSelectedItem()).equals("Name"))
				updateRecipeList(RecipeManager.fetchRecipes(like, approvedBox.isSelected()));
			else if  (((String)jComboBox1.getSelectedItem()).equals("Ingredient"))
				updateRecipeList(RecipeManager.fetchRecipes("%", like.split(", ") , approvedBox.isSelected()));
		}
	}
	
	private void updateRecipeList(ArrayList <Recipe> recipes){
		String[] modelList = new String[recipes.size()];
		Calendar c = new GregorianCalendar();
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d yyyy hh:mm aaa");
		for(int i=0; i<recipes.size(); i++){
			c.setTimeInMillis(recipes.get(i).getCreationDate());
			modelList[i] = "<html><b>"+recipes.get(i).getTitle() + "</b><br><small>ID#"+recipes.get(i).getId()+"</small><br><i>Added on: "+sdf.format(c.getTime())+"</i></html>";
			if(!recipes.get(i).isApproved())
				modelList[i] = "<html>(unapproved) " + modelList[i];
		}
		recipeList.setModel(new DefaultComboBoxModel(modelList));
	}
	private void recipeFilterFieldKeyPressed(KeyEvent evt) throws SQLException {
		getRecipesLike(recipeFilterField.getText());
	}
	
	private void approvedBoxMouseClicked(MouseEvent evt) throws SQLException {
		getRecipesLike(recipeFilterField.getText());
	}
	
	private void prefsBoxMouseClicked(MouseEvent evt) throws SQLException {
		getRecipesLike(recipeFilterField.getText());
	}
	
	private void jCheckBox1MouseClicked(MouseEvent evt) throws SQLException {
		getRecipesLike(recipeFilterField.getText());
	}
	
	private void recipeListMouseClicked(MouseEvent evt) throws SQLException {
		if(recipeList.getSelectedValue() == null) return;
		int rid = Integer.parseInt(((String)recipeList.getSelectedValue()).split("ID#")[1].split("</small>")[0]);
		Recipe r = RecipeManager.getRecipe(rid);
		recipeName.setText(r.getTitle());
		stepsPane.setText(r.getFormattedStepList());
		ingredientPane.setText(r.getFormattedIngredientList());
		if(AccountManager.getLogin().isAdmin() && !r.isApproved()){
			approveButton.setVisible(true);
		} else {
			approveButton.setVisible(false);
		}
	}
	
	private void approveButtonMouseClicked(MouseEvent evt) throws SQLException {
		int rid = Integer.parseInt(((String)recipeList.getSelectedValue()).split("ID#")[1].split("</small>")[0]);
		Recipe r = RecipeManager.getRecipe(rid);
		
		RecipeManager.approveRecipe(r);
		approveButton.setVisible(false);
		getRecipesLike(recipeFilterField.getText());
	}
	
	private void addRecipeButtonMouseClicked(MouseEvent evt) {
		NewRecipeFrame inst = new NewRecipeFrame();
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	}
}
